<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    //1. 数组去重
    let arr = [1,2,4,5,2,1,2,3,4,5,1,2,4];
    //声明一个函数
    let unique = (arg) => {
      return [...new Set(arg)];
    }
    //打印结果
    // console.log(unique(arr));

    let arr1 = [1,2,3,4,5,2];
    let arr2 = [3,4,5,2,6,7,8,2,3,4,5];
    //2. 计算交集
    // let inter = (arg1, arg2) => {
    //   //完整写法
    //   // return [...new Set(arg1)].filter(item => {
    //   //   if((new Set(arg2)).has(item)){
    //   //     return true;
    //   //   }else{
    //   //     return false;
    //   //   }
    //   // });
    //   //简写
    //   return [...new Set(arg1)].filter(item => new Set(arg2).has(item));
    // }

    //再简写
    // let inter = (arg1, arg2) => [...new Set(arg1)].filter(item => new Set(arg2).has(item));
    // console.log(inter(arr1, arr2));

    //3. 计算并集
    let union = (arg1, arg2) => {
      return [...new Set([...arg1, ...arg2])];
    }
    console.log(union(arr1, arr2));

    //4. 计算差集
    let diff = (arg1, arg2) => {
      return [...new Set(arg1)].filter(item => !new Set(arg2).has(item));
    }
    console.log(diff(arr1, arr2));
    console.log(diff(arr2, arr1));


  </script>
</body>
</html>